<?php
/**
* This file is part of Webappkit, a packages system for PHP web apps
* @link http://webappkit.net
* @package dj
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
 * The Dj kit class : use this class for your Dj-derived kits
 * It serves as main frontcontroller for your Dj applications.
 * @todo add middlewares support
 */
class DjKit extends WakKit {

    /**
     * treats current HTTP request
     * sends response to client
     * @param DjHttpRequest $request
     */
    public function run($request=null) {
        // defining request and uri
        if (!$request instanceof DjHttpRequest)
            $request=DjHttpRequest::current();
        // add middleware elements to request (authentication etc)
        // echo '<pre>'; print_r($request);
        // search response for request
        try {
            $response=$this->getResponse($request);
        } catch (DjHttp404 $e) {
            // sends 404 page
            // default or custom content ?
            if (!$response=$this->_templateToResponse('404'))
                $response=new DjHttpResponseNotFound();
        } catch (DjHttp500 $e) {
            // sends 404 page
            // default or custom content ?
            $response=new DjHttpResponseServerError($e->getMessage());
        }
        // sends response
        $this->sendResponse($response);
        //exit;
    }


    /***
     * gets the response for the request
     * may raise DjHttp404
     * @param DjHttpRequest
     * @return DjHttpResponse
     */
    protected function getResponse($request) {
        // getting urlconf from request ?
        // searching route  from urls tool
        if ($route=$this->urls->getRouteForRequest($request)) {
            // getting response from view
            return $this->executeView($route->view,$request,$route->args);
        } else {
            throw new DjHttp404;
        }

    }

    /**
     * execute a view = self method
     *
     * @param string $view view id kit.class.method
     * @param DjHttpRequest $request
     * @param array $args
     * @return DjHttpResponse
     */
    protected function executeView($view, $request, $args=array()) {
        if (!method_exists($this,$view)) {
            return new DjHttpResponseServerError("no method for $view");
        }
        $response=$this->$view($request,$args);
        if (!is_a($response,'DjHttpResponse')) {
            return new DjHttpResponseServerError("method $view do not return a DjHttpResponse");
        }
        return $response;
    }

    /**
     * sends response
     * @param DjHttpResponse $response
     * @return bool
     */
    protected function sendResponse($response) {
        return $response->send();
    }

    /**
     * shortcut
     * @param string $template template id
     * @param mixed $data
     * @return DjHttpResponse
     */
    protected function _templateToResponse($template,$data=array()) {
        $content=$this->templates->get($template)->render($data);
        return new DjHttpResponse($content);
    }
}